package com.ysh.smartplatform.service.impl;

import com.ysh.smartplatform.constans.ResultCode;
import com.ysh.smartplatform.exceptions.FindDataException;
import com.ysh.smartplatform.mapper.OnLineUserMapper;
import com.ysh.smartplatform.pojo.SysUserOnline;
import com.ysh.smartplatform.service.OnLineUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * 袁胜虎 on 2022/9/6 11:35
 */
@Service
@Transactional
public class OnLineUserServiceImpl implements OnLineUserService {

    private OnLineUserMapper onLineUserMapper;

    @Autowired
    public void setOnLineUserMapper(OnLineUserMapper onLineUserMapper) {
        this.onLineUserMapper = onLineUserMapper;
    }

    @Override
    public void addOnlineData(SysUserOnline sysUserOnline) {
        //因为数据来自于我们自己生成的,不是用户传递的,所以我们可以暂时不进行数据校验
        //我们遇到了一个问题,就是对于同一个用户来说,多次登录只需要保存一条数据,所以这个数据如果一开始是存在的,则应该更新
        //先查询数据是否存在,我们的判断方式是用主键查询主键,如果能返回结果,则说明存在
        String loginName = onLineUserMapper.dataExist(sysUserOnline.getLoginName());
        if (StringUtils.hasText(loginName)){
            onLineUserMapper.updateOnlineData(sysUserOnline);
        }else {
            onLineUserMapper.addOnlineData(sysUserOnline);
        }

    }

    @Override
    public void deleteOnline(String loginName) {
        onLineUserMapper.deleteOnline(loginName);
        Assert.notNull(loginName,()->{
            throw  new FindDataException("未找到参数", ResultCode.PARAM_CONTENT_EMPTY);
        });
    }
}
